﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public class NguoiDungDAO
    {
        public static bool KiemTraNguoiDung(NguoiDungDTO user)
        {
            string sql = "select * from NguoiDung where TenDangNhap='" + user.TenDangNhap + "' and MatKhau='" + user.MatKhau + "' and QuyenHan='" + user.QuyenHan + "'";
            DataTable dt = DataAccess.ExcuQuery(sql);
            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public static DataTable LayDanhSachNguoiDung()
        {
            string sql = "select * from NguoiDung";
            return DataAccess.ExcuQuery(sql);
        }

        static public DataTable SelectNguoiDungLikeByChucVu(NguoiDungDTO nd)
        {
            string sql = "select * from NguoiDung where QuyenHan like '%" + nd.QuyenHan + "%'";
            return DataAccess.ExcuQuery(sql);
        }

        public static NguoiDungDTO GetTheLoaiByMa(int Ma)
        {
            string sql = "select * from NguoiDung where MaNguoiDung=" + Ma + "";
            DataTable dt = DataAccess.ExcuQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                NguoiDungDTO nd = new NguoiDungDTO();
                nd.MaNguoiDung = (int)dt.Rows[0].ItemArray[0];
                nd.TenDangNhap = dt.Rows[0].ItemArray[1].ToString();
                nd.MatKhau = dt.Rows[0].ItemArray[2].ToString();
                nd.QuyenHan = dt.Rows[0].ItemArray[3].ToString();
                return nd;
            }

        }

        static public DataTable SelectNguoiDungLikeTen(NguoiDungDTO nd)
        {
            string sql = "select * from NguoiDung where TenDangNhap like '%" + nd.TenDangNhap + "%'";
            return DataAccess.ExcuQuery(sql);
        }

        public static void Insert(NguoiDungDTO nd)
        {
            string sql = "insert into NguoiDung(TenDangNhap,MatKhau,QuyenHan) values('" + nd.TenDangNhap + "','" + nd.MatKhau + "','" + nd.QuyenHan + "')";
            DataAccess.ExcuNonQuery(sql);
        }

        public static void Update(NguoiDungDTO nd)
        {
            string sql = "Update  NguoiDung set TenDangNhap =('" + nd.TenDangNhap + "'),MatKhau=('" + nd.MatKhau + "'), QuyenHan=('" + nd.QuyenHan + "') where MaNguoiDung=" + nd.MaNguoiDung + "";
            DataAccess.ExcuNonQuery(sql);
        }

        public static bool Delete(NguoiDungDTO nd)
        {
            try
            {
                string sql = "delete from NguoiDung where MaNguoiDung= " + nd.MaNguoiDung + "";
                DataAccess.ExcuNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
